﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="App.xaml.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   Interaktionslogik für "App.xaml"
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper
{
    using System;
    using System.Windows;
    using System.Windows.Threading;

    /// <summary>
    ///   Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App
    {
        #region Public Properties

        /// <summary>
        ///   Gets or sets a value indicating whether do handle.
        /// </summary>
        public bool DoHandle { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// The handler for any unhandled exception in the current domain. 
        /// <para>
        /// author: Abhishek Sur <see href="http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications"> Original Source on Code Project </see> 
        /// </para>
        /// </summary>
        /// <param name="sender">
        /// the sender who raised the exception.
        /// </param>
        /// <para>
        /// </para>
        /// <param name="e">
        /// Event - Arguments
        /// </param>
        private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
            if (ex != null)
            {
                MessageBox.Show(ex.Message, "Uncaught Thread Exception", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        /// <summary>
        ///  The handler for any unhandled exception raised under the current dispatcher.
        /// </summary>
        /// <para>
        /// author: Abhishek Sur <see href="http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications"> Original Source on Code Project </see> 
        /// </para>
        /// <param name="sender">
        ///  the sender who raised the exception. 
        /// </param>
        /// <param name="e">
        ///  Event - Arguments
        /// </param>
        private void ApplicationDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            if (this.DoHandle)
            {
                // Handling the exception within the UnhandledExcpeiton handler.
                MessageBox.Show(e.Exception.Message, "Exception Caught", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Handled = true;
            }
            else
            {
                // If you do not set e.Handled to true, the application will close due to crash.
                MessageBox.Show("Application is going to close! ", "Uncaught Exception");
                e.Handled = false;
            }
        }

        /// <summary>
        /// The application startup.
        /// </summary>
        /// <param name="sender">
        /// The sender. 
        /// </param>
        /// <param name="e">
        /// event argument
        /// </param>
        private void ApplicationStartup(object sender, StartupEventArgs e)
        {
            var uri =
                new Uri(
                    "PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component\\themes/aero.normalcolor.xaml", 
                    UriKind.Relative);
            this.Resources.MergedDictionaries.Add(LoadComponent(uri) as ResourceDictionary);

            AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
        }

        #endregion
    }
}